package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping(value = "/spu/page",name = "分页查询spu数据")
    public ResponseEntity<PageResult<SpuDTO>> findSpuByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){
        PageResult<SpuDTO> pageResult = goodsService.findSpuByPage(page,rows,key, saleable);
//        return ResponseEntity.ok().body(pageResult);
        return ResponseEntity.ok(pageResult);

    }

    @PostMapping(value = "/goods",name = "保存商品信息")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    @PutMapping(value = "/goods",name = "修改商品信息")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    @PutMapping(value = "/spu/saleable",name = "修改商品上下架")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id,@RequestParam("saleable") Boolean saleable ){
        goodsService.updateSaleable(id,saleable);
        return ResponseEntity.ok().build();
    }
//    GET /spu/detail?id=2

    @GetMapping(value = "/spu/detail",name = "根据spuId查询SPUDetail")
    public ResponseEntity<SpuDetailDTO> findSpuDetailBySpuId(@RequestParam("id") Long id){
        SpuDetailDTO spuDetailDTO = goodsService.findSpuDetailBySpuId(id);
        return ResponseEntity.ok(spuDetailDTO);
    }
//    GET /sku/of/spu?id=1
    @GetMapping(value = "/sku/of/spu",name = "根据spuId查询sku列表数据")
    public ResponseEntity<List<SkuDTO>> findSkuListBySpuId(@RequestParam("id") Long id){
        List<SkuDTO> skuDTOList = goodsService.findSkuListBySpuId(id);
        return ResponseEntity.ok(skuDTOList);
    }
//    GET /spu/{id}  RESTFUL风格
//    根据spuId查询spu对象
    @GetMapping(value = "/spu/{id}",name = "根据spuId查询spu对象")
    public ResponseEntity<SpuDTO> findSpuBySpuId(@PathVariable("id") Long id){
        SpuDTO spuDTO = goodsService.findSpuBySpuId(id);
        return ResponseEntity.ok(spuDTO);
    }
    @GetMapping(value = "/sku/list",name = "根据skuId集合查询sku对象集合")
    public ResponseEntity<List<SkuDTO>> findSkuListBySkuIds(@RequestParam("ids") List<Long> ids){
        List<SkuDTO> skuDTOListkuDTO = goodsService.findSkuListBySkuIds(ids);
        return ResponseEntity.ok(skuDTOListkuDTO);
    }



    @PutMapping(value = "/stock/minus",name = "减库存")
    public ResponseEntity<Void> stockMinus(@RequestBody Map<Long,Integer> skuIdAndNumMap){
        goodsService.stockMinus(skuIdAndNumMap);
        return ResponseEntity.ok().build();
    }

    @PutMapping(value = "/stock/plus",name = "恢复库存")
    public ResponseEntity<Void> stockPlus(@RequestBody Map<Long,Integer> skuIdAndNumMap){
        goodsService.stockPlus(skuIdAndNumMap);
        return ResponseEntity.ok().build();
    }

//    /spu/for/brand?id=18374&cid=76
    @GetMapping(value = "/spu/for/brand",name = "分页查询spu数据")
    public ResponseEntity<List<SpuDTO>> findSpuByBrandIdAndCategoryId(
            @RequestParam(value = "id") Long brandId ,
            @RequestParam(value = "cid") Long cid
    ){
        List<SpuDTO> spuDTOList = goodsService.findSpuByPage(brandId,cid);
        return ResponseEntity.ok(spuDTOList);

    }

}
